#!/usr/bin/python
# -*- coding: UTF-8 -*-
import database
import medicinevector
import sys
import collections
import json
import copy
import numpy as np
import pandas as pd

reload(sys)
sys.setdefaultencoding('utf8')

class Anagraph:
    def __init__(self):
        self.obj_med  = database.Medicine()
        self.obj_vec  = medicinevector.MedicineVector()
        self.med_dict = collections.OrderedDict()

        self.__getMedDict()

    def __getMedDict(self):
        prescriptions = self.obj_med.getAllAnagraphComposes('')

        for (ana_name, med_list) in prescriptions.items():
            for med_name in med_list:
                if not self.med_dict.has_key(med_name):
                    self.med_dict[med_name] = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]

        return self.med_dict

    def getAnagraphs(self, str_origin = ''):
        all_anagrphs = []

        prescriptions = self.obj_med.getAllAnagraphComposes(str_origin)
        for (ana_name, med_list) in prescriptions.items():
            tmp_dict = {}
            tmp_dict['name'] = ana_name
            tmp_dict['medicines'] = med_list

            tmp_template = copy.deepcopy(self.med_dict)

            for med_name in med_list:
                try:
                    std_vector = self.obj_vec.getStanardMedicine(med_name)
                    tmp_template[med_name] = std_vector

                except ValueError, e:
                    print e

            tmp_dict['vector'] = tmp_template.values()

            all_anagrphs.append(tmp_dict)

        return all_anagrphs
